package com.gfhd.multithread.thread_comunication;

import lombok.Data;

import java.util.concurrent.locks.ReentrantLock;

@Data
public class Account {
    private String cardId;
    private double money;

    public Account(String cardId, double money) {
        this.cardId = cardId;
        this.money = money;
    }

    public Account() {
    }

    public static void run(){
        synchronized (Account.class){

        }
    }

    public synchronized void deposit(double money) {
        try {
            String name = Thread.currentThread().getName();
            if(this.money==0){
                this.money += money;
                System.out.println(name+"来存钱"+money+"成功！余额是："+this.money);
                this.notifyAll();
                this.wait();
            }else {
                this.notifyAll();
                this.wait();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public synchronized void drawMone(double money) {
        try {
            String name = Thread.currentThread().getName();
            if (this.money>=money){
                this.money -= money;
                System.out.println(name+"来取钱"+money+"成功！余额是："+this.money);
            }else {
                this.notifyAll();
                this.wait();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
